package com.jftone.bh.app.action;

import com.jftone.bh.app.model.NtbBaseCoach;
import com.jftone.bh.app.model.SysArea;
import com.jftone.bh.app.service.AreaService;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.Page;
import org.jftone.util.StringUtil;

import java.util.List;


@Controller(mapping="/areaAction")
public class AreaAction extends BaseAction {

	@Autowired
	private AreaService service;

	private Logger logger = Logger.getLogger(AreaAction.class);

	/**
	 * 获取省份
	 * @throws ActionException
	 */
	public void query() throws ActionException {
		IData<String, Object> data = getData();
		try {
			//设置功能按钮
			setMenuFuncDerictive();
			//设置分页组件
			setPageDerictive();
			Page page = getPage();
            page.setPageSize(10);
			List<SysArea> areaList = service.loadAreaList(page);
			this.putRenderInfo("areaList", areaList);
			this.render("province/province_list.html");
		} catch (Exception e) {
			logger.error("加载省份列表错误", e);
			throw new ActionException("加载省份列表数据错误", e);
		}
	}
    public void edit() throws ActionException {
        IData<String, Object> data = getData();
        String dataId = data.getString("dataId");
        if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
            this.alertMsg("参数传值错误");
            return;
        }
        try {
            IData<String, Object> inData = new DataMap<String, Object>();
            inData.put("id", Integer.parseInt(dataId));
            SysArea area = service.getSysArea(inData);
            this.putRenderInfo("area", area);
            this.render("province/province_form.html");
        } catch (Exception e) {
            logger.error("编辑省份数据错误"+e.getMessage(), e);
            throw new ActionException("编辑省份信息错误"+e.getMessage(), e);
        }
    }

    public void save() throws ActionException{
        IData<String, Object> data = getData();
        String areaId = data.getString("areaId");
        if(!StringUtil.isBlank(areaId) && !StringUtil.isNumber(areaId)){
            this.alertMsg("参数传值错误");
            return;
        }
        try {
            int resCode = service.saveSysArea(data);
            this.showMsg("省份信息保存成功", "?method=query");
        } catch (Exception e) {
            logger.error("省份基站信息错误"+e.getMessage(), e);
            throw new ActionException("省份基站信息错误"+e.getMessage(), e);
        }
    }

}
